package com.ruoyi.quartz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quartz.domain.SysJobLog;
import com.ruoyi.quartz.mapper.SysJobLogMapper;
import com.ruoyi.quartz.service.ISysJobLogService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 定时任务调度日志信息 服务层
 *
 * @author ruoyi
 */
@Service
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog> implements ISysJobLogService {

    /**
     * 获取quartz调度器日志的计划任务
     *
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public List<SysJobLog> selectJobLogList(SysJobLog jobLog) {
        LambdaQueryWrapper<SysJobLog> lqw = new LambdaQueryWrapper<>();

        if (jobLog != null) {
            if (StringUtils.isNotBlank(jobLog.getJobName())) {
                lqw.like(SysJobLog::getJobName, jobLog.getJobName());
            }
            if (StringUtils.isNotBlank(jobLog.getJobGroup())) {
                lqw.eq(SysJobLog::getJobGroup, jobLog.getJobGroup());
            }
            if (StringUtils.isNotBlank(jobLog.getStatus())) {
                lqw.eq(SysJobLog::getStatus, jobLog.getStatus());
            }
            if (StringUtils.isNotBlank(jobLog.getInvokeTarget())) {
                lqw.like(SysJobLog::getInvokeTarget, jobLog.getInvokeTarget());
            }

            // 处理时间范围查询
            if (jobLog.getParams() != null) {
                String beginTime = (String) jobLog.getParams().get("beginTime");
                String endTime = (String) jobLog.getParams().get("endTime");

                if (StringUtils.isNotBlank(beginTime)) {
                    lqw.ge(SysJobLog::getCreateTime, beginTime);
                }
                if (StringUtils.isNotBlank(endTime)) {
                    lqw.le(SysJobLog::getCreateTime, endTime);
                }
            }
        }

        lqw.orderByDesc(SysJobLog::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询所有调度任务日志
     *
     * @return 调度任务日志列表
     */
    public List<SysJobLog> selectJobLogAll()
    {
        return baseMapper.selectList(Wrappers.emptyWrapper());
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(Long jobLogId) {
        return baseMapper.selectById(jobLogId);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog) {
        baseMapper.insert(jobLog);
    }

    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(Long[] logIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(logIds));
    }

    /**
     * 删除任务日志
     *
     * @param jobId 调度日志ID
     */
    @Override
    public int deleteJobLogById(Long jobId) {
        return baseMapper.deleteById(jobId);
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog() {
        // 使用 LambdaQueryWrapper 删除所有记录
        LambdaQueryWrapper<SysJobLog> wrapper = new LambdaQueryWrapper<>();
        // jobLogId 是主键，这个条件永远为真
        wrapper.isNotNull(SysJobLog::getJobLogId);
        remove(wrapper);
    }
}
